package com.user.action

import com.data.annotation.NoargAnnotation
import com.data.annotation.OpenAnnotation
import com.data.bean.ThemeScreenshot
import com.data.util.CheckDataUtils
import com.data.util.ImageLookUtils
import com.data.util.JsonUtils
import com.user.service.ThemeScreenshotAdminService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.commons.CommonsMultipartFile

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * FileName: ThemeScreenshotAdminAction
 * Author:   HingLo
 * Date:     2017/11/6 12:01
 * Description: 截图管理
 */
@Controller
@OpenAnnotation
@NoargAnnotation
@RequestMapping("themeScreenshot/user/")
class ThemeScreenshotAdminAction {
    @Autowired
    var themeScreenshotAdminService: ThemeScreenshotAdminService? = null

    /**
     * 根据id查询截图
     *
     * @param tid 主题的id
     * @param request
     * @return
     */
    @RequestMapping("list")
    fun listThemeScreenshotAdminActon(@RequestParam("tid")tid: String, request: HttpServletRequest): String {
        val list = this.themeScreenshotAdminService!!.listThemeScreenshotAdminService(tid)
        request.setAttribute("dataMap", list)
        return "WEB-INF/user/themeScreenshot/listThemeScreenshot"
    }

    /**
     * 预览时候查询的截图
     */
    @RequestMapping("lists")
    fun listsThemeScreenshotAdminActon(@RequestParam("tid")tid: String, request: HttpServletRequest): String {
        val list = this.themeScreenshotAdminService!!.listThemeScreenshotAdminService(tid)
        request.setAttribute("dataMap", list)
        return "screenshot"
    }


    /**
     * 查看图片
     *
     * @param image    图片的名称
     * @param response
     */
    @GetMapping("image")
    fun showImageAction(@RequestParam("image")image: String,@RequestParam("tid") tid: String, response: HttpServletResponse) {
        val inputStream = themeScreenshotAdminService!!.showImageService(image, tid)
        //响应图片
        if (inputStream != null)
            ImageLookUtils.responseImage(inputStream, response)

    }

    /**
     * 根据id删除未上线的主题截图
     *
     * @param id      截图id
     * @param tid     截图所属的主题id
     * @param request
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    fun deleteThemeScreenshotAdminActon(@RequestParam("id")id: String, @RequestParam("tid")tid: String, request: HttpServletRequest): Map<String, Any> {
        val bool = this.themeScreenshotAdminService!!.deleteThemeScreenshotAdminService(id, tid)
        return JsonUtils.returnMassageUtils(bool, if (bool) "删除成功" else "删除失败")
    }

    /**
     * 进入到添加截图界面
     */
    @RequestMapping("add")
    fun addThemeScreenshotAdminActon(@RequestParam("tid")tid: String, request: HttpServletRequest): String {
        request.setAttribute("tid", tid)
        return "WEB-INF/user/themeScreenshot/addThemeScreenshot"
    }

    /**
     * 添加截图
     *
     * @param themeScreenshot 截图实体信息
     * @param image           截图
     * @param request
     * @return
     */
    @RequestMapping("submit")
    @ResponseBody
    fun submitThemeScreenshotAdminActon(themeScreenshot: ThemeScreenshot, @RequestParam("images") images: CommonsMultipartFile, request: HttpServletRequest): Map<String, Any> {
        val bool = this.themeScreenshotAdminService!!.submitThemeScreenshotAdminService(themeScreenshot, images)
        return JsonUtils.returnMassageUtils(bool, if (bool) "添加成功" else "添加失败")
    }

    /**
     * 更换截图
     * @param imageName: 图片的名称
     * @param  id 截图的唯一id
     * @param  file 更换的文件图片的对象
     *
     * @return
     * *
     */
    @PostMapping("changeThemeScreenshotAdmin")
    @ResponseBody
    fun changeThemeScreenshotAdminActon(@RequestParam("image") image: CommonsMultipartFile, @RequestParam("id") id: String, @RequestParam("tid") tid: String, @RequestParam("value") value: String): Map<String, Any> {
        var bool = false
        if (CheckDataUtils.stringUtilsID(id)) {
            bool = this.themeScreenshotAdminService!!.changeThemeScreenshotAdminService(image, id, tid, value)
        }

        return JsonUtils.returnMassageUtils(bool, if (bool) "修改成功" else "修改失败")
    }

    /**
     * 修改截图的信息
     * @param value: 修改后的值
     * @param  id 截图的唯一id
     * @param  type 修改的类型
     *
     * @return
     * *
     */
    @PostMapping("changeThemeScreenshotInfoAdmin")
    @ResponseBody
    fun changeThemeScreenshotInfoAdminActon(@RequestParam("type") type: Int, @RequestParam("id") id: String, @RequestParam("value") value: String): Map<String, Any> {
        var bool = false
        if (CheckDataUtils.stringUtilsID(id)) {
            bool = this.themeScreenshotAdminService!!.changeThemeScreenshotInfoAdminService(type, id, value)
        }
        return JsonUtils.returnMassageUtils(bool, if (bool) "修改成功" else "修改失败")
    }

    /**
     * 修改截图的顺序
     * @param id: id列表
     *
     * @return
     * *
     */
    @PostMapping("changeThemeScreenshotSoftAdmin")
    @ResponseBody
    fun changeThemeScreenshotSoftAdminActon(@RequestParam("id") id: String): Map<String, Any> {
        val bool: Boolean = this.themeScreenshotAdminService!!.changeThemeScreenshotSoftAdminService(id)
        return JsonUtils.returnMassageUtils(bool, if (bool) "修改成功" else "修改失败")
    }


}
